// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        
        icon: {
            default: null,
            type: cc.Sprite
        },

        iconTextures: {
            default: [],
            type: [cc.SpriteFrame]
        },

        moveSpeed: 0,
        speedSnap: 0,
    },

    initInfo(info, manager)
    {
        // if(level < this.iconTextures.length)this.level = level;
        // else this.level = this.iconTextures.length - 1;
        this.info = info;
        cc.log(info.icon + ':' + info.xArray.length);
        this.manager = manager;
        let grid = this.node.getComponent('GridManager');
        this.node.getComponent(cc.PhysicsBoxCollider).size.width = info.xArray.length * grid.cellWidth;
        grid.setMaxCount(info.xArray.length, this.iconTextures[parseInt(info.icon)-1]);
    },
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.info = null;
    },

    start () {

    },

    update (dt) {
        this.node.y += this.info.velocity * dt;
    },

    onCollisionEnter: function(other, self){
        if (other.node.group != 'BulletWall') {
            return;
        }

        if (other.node.group == 'BulletWall') {
            //this.node.removeFromParent();
            this.manager.delBullet(this.node);
            // cc.log('Destroy Bullet on Wall');
        }
    },

    onBeginContact: function (contact, selfCollider, otherCollider) {

        if (otherCollider.node.group == 'BulletWall' || otherCollider.node.group == 'Enemy') {
            //this.node.removeFromParent();
            this.manager.delBullet(this.node);
            // cc.log('Destroy Bullet on Wall');
        }
    },
});
